package com.dirven.game.sprite;

import com.dirven.game.Coord;
import com.dirven.game.DirVenGame;
import com.dirven.game.Misc;

public class TunnelPhoton extends Sprite {

	public final static int PHOTONSIZE_32 = 32,
							PHOTONSIZE_64 = 64,
							PHOTONSIZE_128= 128,
							PHOTONSIZE_256= 256;
	
	int direction;
	
	int dx = 0,
		dy = 0;
	
	float speed = 1f;
	
	public TunnelPhoton(float x, float y, int photonSize, int direction) {
		
		super(x, y, Sprite.TYPE_BACKGROUND, "tunnel", "blue-photon-"+photonSize+".png");
		
		randomizeSpeed();
		
		this.direction = direction;
		
		switch(direction) {
			case Sprite.DIRECTION_NORTH: 
				dx = 0;
				dy = 1;
			break;
			
			case Sprite.DIRECTION_SOUTH: 
				dx = 0;
				dy = -1;
				// Flip vertically
				//super.flip(false, true);
				super.rotate(-180.0f);
			break;
			
			case Sprite.DIRECTION_EAST: 
				dx = 1;
				dy = 0;
				// Rotate 90 degrees
				super.rotate(-90.0f);
			break;
			
			case Sprite.DIRECTION_WEST: 
				dx = -1;
				dy = 0;
				// Rotate 90 degrees counter clock wise
				super.rotate(90.0f);
			break;
		}
		
	}
	
	private void resetOrientation() {
		
		switch(direction) {
			case Sprite.DIRECTION_NORTH: 
			break;
			
			case Sprite.DIRECTION_SOUTH: 
				// Flip vertically
				//super.flip(false, false);
				super.rotate(180.0f);
			break;
			
			case Sprite.DIRECTION_EAST: 
				super.rotate(90.0f);
			break;
			
			case Sprite.DIRECTION_WEST: 
				super.rotate(-90.0f);
			break;
		}
	}
	
	public void setDirection(int newDirection) {
		resetOrientation();

		switch(newDirection) {
			case Sprite.DIRECTION_NORTH: 
				dx = 0;
				dy = 1;
			break;
			
			case Sprite.DIRECTION_SOUTH: 
				dx = 0;
				dy = -1;
				// Flip vertically
				//super.flip(false, true);
				super.rotate(-180.0f);
			break;
			
			case Sprite.DIRECTION_EAST: 
				dx = 1;
				dy = 0;
				// Rotate 90 degrees
				super.rotate(-90.0f);
			break;
			
			case Sprite.DIRECTION_WEST: 
				dx = -1;
				dy = 0;
				// Rotate 90 degrees counter clock wise
				super.rotate(90.0f);
			break;
		}
		
		this.direction = newDirection;
	}
	
	private void randomizeSpeed() {

		speed = (float)Misc.randomNumber(1, 5);		
	}
	
	public void reset(int direction) {
		randomizeSpeed();
		setDirection(direction);
	}
	
	public void update() {
		move();
	}

	private void move() {
		
		super.shiftPosition((float)dx*speed, (float)dy*speed);
			
		if(isOffscreen()) {
			super.despawn();
		}
	}
	
	private boolean isOffscreen() {
		Coord newPos = super.getPosition();
		float width = super.getWidth();
		float height = super.getHeight();
		if(dy > 0 && newPos.y > DirVenGame.screenHeight) {
			return true;
		}
		if(dy < 0 && newPos.y < -height) {
			return true;
		}
		if(dx > 0 && newPos.x > DirVenGame.screenWidth) {
			return true;
		}
		if(dx < 0 && newPos.x < -width) {
			return true;
		}
		return false;
	}
}
